package cn.dengta.context.rpc;

import cn.dengta.common.web.Client;
import cn.dengta.common.web.RequestClientResolver;
import cn.dengta.context.model.Language;
import cn.dengta.context.model.WesKit;
import cn.dengta.context.web.ProxyRequest;
import lombok.Getter;
import lombok.Setter;
import me.codeplayer.util.StringUtil;
import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.RpcServiceContext;

public class RpcContextUtil {

	public static final String USER_ID = "user_id";
	public static final String WES_KIT = "wes_kit";
	public static final String LANGUAGE = "language";
	public static final String CLIENT = "client";
	public static final String APP_ID = "app_id";


	public static RpcContextAttr getRpcContextAttr() {
		RpcContextAttr attr = new RpcContextAttr();
		RpcServiceContext context = RpcContext.getCurrentServiceContext();
		if (context == null) {
			return new RpcContextAttr();
		}
		String userId = context.getAttachment(USER_ID);
		if (StringUtil.notEmpty(userId)) {
			attr.setUserId(Long.parseLong(userId));
		}
		final String appId = context.getAttachment(APP_ID);
		if (StringUtil.notEmpty(appId)) {
			attr.setAppId(appId);
		}
		String kit = context.getAttachment(WES_KIT);
		if (StringUtil.notEmpty(kit)) {
			attr.setWesKit(WesKit.of(Integer.parseInt(kit)));
		}
		final String client = context.getAttachment(CLIENT);
		if (StringUtil.notEmpty(client)) {
			attr.setClient(Client.of(Integer.parseInt(client)));
		}
		String lang = context.getAttachment(LANGUAGE);
		if (StringUtil.notEmpty(lang)) {
			attr.setLanguage(Language.of(Integer.parseInt(lang)));
		}
		return attr;
	}

	private static void setRpcContextAttr(RpcContextAttr attr) {
		RpcServiceContext context = RpcContext.getCurrentServiceContext();
		if (context == null) {
			return;
		}
		if (attr.getClient() != null) {
			context.setAttachment(USER_ID, attr.getUserId());
		}
		if (StringUtil.notEmpty(attr.getAppId())) {
			context.setAttachment(APP_ID, attr.getAppId());
		}
		if (attr.getWesKit() != null) {
			context.setAttachment(WES_KIT, attr.getWesKit().getValue());
		}
		if (attr.getLanguage() != null) {
			context.setAttachment(LANGUAGE, attr.getLanguage().getValue());
		}
	}

	public static RpcContextAttr init(Long userId, Language language, String appId) {
		RpcContextAttr attr = new RpcContextAttr();
		attr.setUserId(userId);
		attr.setLanguage(language);
		attr.setAppId(appId);
		setRpcContextAttr(attr);
		return attr;
	}

	@Getter
	@Setter
	public static class RpcContextAttr {

		/** 用户ID */
		private Long userId;
		/** 分包数据 优先级高于 App-Id */
		private WesKit wesKit;
		/** 当前语言 */
		private Language language;
		/** 当前客户端 优先级高于 App-Id */
		private Client client;
		/** App-id */
		private String appId;

		public String getVersion() {
			if (StringUtil.notEmpty(appId)) {
				return ProxyRequest.getVersion(appId);
			}
			return null;
		}

		public void setAppId(String appId) {
			this.appId = appId;
			this.client = RequestClientResolver.findClientByAppId(appId);
			this.wesKit = WesKit.ofAppId(appId, false);
		}

	}

}
